package ch09.game;


import java.io.*;
import java.util.List;

public class ObjectStreamManager {

    public static void save(Position position){
        try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("data/game2.save"))) {
            oos.writeObject(position);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Position load(){
        System.out.println("ObjectStreamManager...");
        Position position = new Position(0,0);
        try(ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("data/game2.save"))){
            Object obj = objectInputStream.readObject();
            position = (Position) obj;
        } catch (FileNotFoundException e) {
//            e.printStackTrace();
            System.out.println("没有读取到文件，使用默认值");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return position;
    }
}
